<!--
 * @Author: xuna
 * @Descripttion: 公告资讯模块
-->
<template>
  <div class="notice-container">
    <el-form ref="formInline" :inline="true" :model="noticeInfo" :label-position="labelPosition">
      <el-row type="flex" justify="space-between">
        <el-col :sm="6">
          <el-form-item label="内容关键词:" prop="content">
            <el-input
              v-model="noticeInfo.content"
              placeholder="请输入关键词"
              class="normal-input"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="10" type="flex" justify="center" style="display:flex;justify-content:center">
          <el-form-item label="创建日期:" prop="startTime">
            <!-- <el-row type="flex"> -->
            <!-- <el-col class="sign-time"> -->
            <!-- :editable="false" prefix-icon="null" -->
            <el-date-picker
              v-model="noticeInfo.startTime"
              style="width: 180px"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="开始日期"
              :picker-options="statrPickOption"
            ></el-date-picker>
            ~
            <!-- <i class="el-icon-caret-bottom"></i> -->
            <!-- </el-col>
              <el-col class="tag-icon"><i></i></el-col> -->
            <!-- <el-col class="sign-time"> -->
            <el-date-picker
              v-model="noticeInfo.endTime"
              style="width: 180px"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="结束日期"
              :picker-options="endPickOption"
            ></el-date-picker>
            <!-- <i class="el-icon-caret-bottom"></i> -->
            <!-- </el-col> -->
            <!-- </el-row> -->
          </el-form-item>
        </el-col>
        <el-col class="share-input" :sm="4">
          <el-form-item label="分享:" prop="isShare">
            <el-select v-model="noticeInfo.isShare" placeholder="请选择" class="normal-input">
              <el-option
                v-for="item in shareList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="space-between">
        <el-col>
          <el-form-item label="状态:" prop="status">
            <el-select v-model="noticeInfo.status" placeholder="请选择状态" class="normal-input">
              <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="setting-btn">
          <el-form-item>
            <el-button class="settbtn reset-btn" @click="resetForm()">重置</el-button>
            <el-button class="settbtn search-btn" type="primary" @click="getTableList()">
              查询
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row type="flex" justify="end" class="notice-row">
      <wf-el-auth-button
        auth-key="WALFARE-GLZYGL-XZ"
        type="primary"
        class="add-notice"
        @click="handleClick('newlyAdd')"
      >
        新增公告资讯
      </wf-el-auth-button>
    </el-row>
    <!-- 表格 -->
    <div class="page-table-container">
      <wf-el-table
        :list="tableData"
        :total="total"
        :loading="loading"
        :current="current"
        @on-page-change="onPageChange"
      >
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column
          v-for="item in propData"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
        ></el-table-column>
        <el-table-column fixed="right" width="257px" label="操作">
          <template slot-scope="scope">
            <wf-el-auth-button
              v-if="scope.row.status == '已发布'"
              auth-key="WALFARE-RCFL-FBXX"
              type="text"
              class="handle-icon"
              @click="dialogClick(0, scope.row)"
            >
              <img src="@/assets/img/down_icon.png" alt="" />
              下线
            </wf-el-auth-button>
            <wf-el-auth-button
              v-if="scope.row.status == '未发布'"
              auth-key="WALFARE-RCFL-FBXX"
              type="text"
              class="handle-icon"
              @click="dialogClick(1, scope.row)"
            >
              <img src="@/assets/img/down_icon.png" class="scale-img" alt="" />
              上线
            </wf-el-auth-button>

            <wf-el-auth-button
              auth-key="WALFARE-GLZYGL-BJ"
              type="text"
              class="handle-icon"
              @click="handleClick('edit', scope.row)"
            >
              <img src="@/assets/img/edit_icon.png" alt="" />
              编辑
            </wf-el-auth-button>

            <wf-el-auth-button
              v-if="scope.row.status == '未发布'"
              auth-key="WALFARE-GLZYGL-SC"
              type="text"
              class="handle-icon"
              @click="dialogClick(2, scope.row)"
            >
              <img src="@/assets/img/delete_icon.png" alt="" />
              删除
            </wf-el-auth-button>
          </template>
        </el-table-column>
      </wf-el-table>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>{{ dialogTip }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmOnlineoff">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增、编辑公告资讯组件 -->
    <Detail
      :is-open-detail="openEdit"
      :notice-row="noticeRow"
      :is-edit="isEdit"
      @successEdit="successEdit"
      @closeDrawer="closeDrawer"
    ></Detail>
  </div>
</template>

<script>
  import WfElTable from '@/components/Table/index';
  import Detail from './components/Detail';
  import { getNoticeList, onlineOrOffLine, noticeDelete } from '@/api/noticeinformation';
  export default {
    components: { WfElTable, Detail },
    data() {
      return {
        current: 1,
        total: 0,
        openEdit: false,
        isEdit: false,
        isOpenRecover: false,
        loading: false,
        noticeRow: {},
        recoverInfo: {},
        defaultEndtime: '',
        tableData: [],
        propData: [
          {
            prop: 'title',
            label: '标题',
            width: '200',
          },
          {
            prop: 'content',
            label: '内容',
            width: '500',
          },
          {
            prop: 'type',
            label: '类型',
          },
          {
            prop: 'isShare',
            label: '是否允许分享',
            width: '150',
          },
          {
            prop: 'status',
            label: '状态',
          },
          {
            prop: 'createUsername',
            label: '创建人',
          },
          {
            prop: 'createTime',
            label: '创建时间',
          },
        ],
        noticeInfo: {
          content: '',
          startTime: null,
          endTime: null,
          isShare: null,
          status: null,
        },
        statusList: [
          {
            label: '全部',
            value: null,
          },
          {
            label: '已发布',
            value: 1,
          },
          {
            label: '未发布',
            value: 0,
          },
        ],
        shareList: [
          {
            label: '全部',
            value: null,
          },
          {
            label: '是',
            value: 1,
          },
          {
            label: '否',
            value: 0,
          },
        ],
        labelPosition: 'right',
        dialogTip: '',
        dialogVisible: false,
        statrPickOption: {
          disabledDate: time => {
            if (this.noticeInfo.endTime != null) {
              return (
                time.getTime() > new Date(this.noticeInfo.endTime).getTime() - 8.64e6 ||
                time.getTime() < 1577808000000 ||
                time.getTime() > 1609344000000
              );
            } else {
              return time.getTime() < 1577808000000 || time.getTime() > 1609344000000;
            }
          },
        },
        endPickOption: {
          disabledDate: time => {
            if (this.noticeInfo.startTime != null) {
              return (
                time.getTime() < new Date(this.noticeInfo.startTime).getTime() - 8.64e7 ||
                time.getTime() < 1577808000000 ||
                time.getTime() > 1609344000000
              );
            } else {
              return time.getTime() < 1577808000000 || time.getTime() > 1609344000000;
            }
          },
        },
        curNoticeId: '',
        curVersion: '',
        curHandle: null,
      };
    },
    activated() {
      this.getTableList();
    },
    methods: {
      initList(noticeInfo, current = this.current) {
        this.loading = true;
        console.log(noticeInfo.startTime);
        console.log(noticeInfo);
        noticeInfo.startTime = noticeInfo.startTime
          ? `${noticeInfo.startTime} 00:00:00`
          : noticeInfo.startTime;
        noticeInfo.endTime = noticeInfo.endTime
          ? `${noticeInfo.endTime} 23:59:59`
          : noticeInfo.endTime;
        let objData = {
          current,
          size: 10,
          ...noticeInfo,
        };
        try {
          getNoticeList(objData).then(res => {
            this.loading = false;
            console.log(res);
            if (res.status == 200) {
              let tableData = JSON.parse(JSON.stringify(res.data.records));
              this.tableData = tableData.map(val => {
                val.type = val.type == 1 ? '文本' : val.type == 2 ? '图片' : '视频';
                val.status = val.status == 0 ? '未发布' : '已发布';
                val.isShare = val.isShare ? '允许' : '不允许';
                return val;
              });
              this.total = res.data.total;
            }
          });
        } catch (error) {
          this.loading = false;
        }
      },
      getTableList() {
        this.initList(this.noticeInfo, 1);
      },
      async resetForm() {
        this.$refs.formInline.resetFields();
        this.noticeInfo.endTime = await null;
        this.getTableList();
      },
      handleClick(flag, row) {
        console.log(row, flag);
        this.openEdit = true;
        if (flag == 'newlyAdd') {
          // 新增
          this.isEdit = false;
          this.noticeRow = {};
        } else {
          // 编辑
          this.isEdit = true;
          this.noticeRow = row;
        }
      },
      closeDrawer(close) {
        this.openEdit = close;
        this.noticeRow = {};
        this.isEdit = false;
      },
      successEdit(val) {
        this.openEdit = val;
        this.initList(this.noticeInfo, this.current);
      },
      dialogClick(index, row) {
        console.log(row);
        this.curNoticeId = row.id;
        this.curVersion = row.version;
        this.curHandle = index;
        if (index == 1) {
          this.dialogTip = '上线后，员工即可查看该公告资讯信息，您是否确认上线操作？';
        } else if (index == 0) {
          this.dialogTip = '下线后，员工将无法查看该公告资讯信息，您是否确认下线操作？';
        } else if (index == 2) {
          this.dialogTip = '删除后，列表不再保留此公告信息，您是否确认删除？';
        }
        this.dialogVisible = true;
      },
      confirmOnlineoff() {
        if (this.curHandle < 2) {
          // 上下线
          let tips = this.curHandle ? '上线' : '下线';
          let objData = {
            id: this.curNoticeId,
            status: this.curHandle,
            version: this.curVersion,
          };
          console.log(objData, '---------------');
          onlineOrOffLine(objData).then(res => {
            if (res.status == 200) {
              if (this.total % 10 == 1) {
                this.current--;
              }
              this.initList(this.noticeInfo, this.current);
              this.dialogVisible = false;
              this.$message({
                type: 'success',
                message: `${tips}成功!`,
              });
            }
          });
        } else {
          // 删除
          noticeDelete({ id: this.curNoticeId }).then(res => {
            if (res.status == 200) {
              if (this.total % 10 == 1) {
                this.current--;
              }
              this.initList(this.noticeInfo, this.current);
              this.dialogVisible = false;
              this.$message({
                type: 'success',
                message: `删除成功!`,
              });
            }
          });
        }
      },
      onPageChange(data) {
        this.current = data.current;
        this.initList(this.noticeInfo, this.current);
      },
    },
  };
</script>

<style lang="scss" scoped>
  @media only screen and (min-width: 768px) {
    .page-table-container {
      height: calc(100% - 246px);
    }
  }
  @media only screen and (min-width: 1900px) {
    .page-table-container {
      height: calc(100% - 206px);
    }
  }
  .page-table-container {
    /deep/.cell {
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

  .wf-table-wrapper {
    height: 100% !important;
  }
  @import '~@/styles/mixin.scss';
  .notice-container {
    background-color: #efeef1;
    @include wh(100%, 100%);
    overflow: hidden;
    padding-top: 0 !important;
    /deep/.el-form--inline {
      background-color: #fff;
      border-radius: 7px;
      padding: 14px 17px 0 30px;
      box-sizing: border-box;
    }

    /deep/.el-form-item__label {
      @include font(14px, #7f848b, 400);
    }
    /deep/.el-input {
      background: rgba(255, 255, 255, 1);
      border-radius: 7px;
      border: 1px solid rgba(244, 240, 243, 1);
    }
    /deep/.el-table {
      border-radius: 7px;
    }
    .setting-btn {
      display: flex;
      justify-content: flex-end;
    }
    .share-input {
      display: flex;
      justify-content: flex-end;
    }
    .settbtn {
      @include wh(94px, 36px);
      border-radius: 7px;
    }
    .reset-btn {
      background: rgba(255, 254, 255, 1);
      border: 1px solid rgba(232, 228, 232, 1);
    }
    .search-btn {
      background-color: #3771e9;
    }
    .normal-input {
      @include wh(210px, 36px);
    }
    .data-select {
      width: 300px;
    }
    .bottom-panel {
      padding-left: 40px;
      box-sizing: border-box;
    }
    .notice-row {
      @include wh(100%, 74px);
      display: flex;
      align-items: center;
    }
    .add-notice {
      @include wh(116px, 36px);
      background: rgba(55, 113, 233, 1);
      border-radius: 7px;
    }
    .handle-icon {
      @include font(14px, #353739, 400);
      margin-right: 15px;
      img {
        @include wh(23px, 23px);
        vertical-align: middle;
        &.scale-img {
          transform: rotate(180deg);
        }
      }
    }
  }
  .balance {
    width: 385px;
    .el-input {
      width: 140px;
    }
  }
  // /deep/.wf-table-wrapper {
  //   height: calc(100vh - 106px) !important;
  // }
  // /deep/.wf-table-content {
  //   height: calc(100% - 206px);
  // }
  .sign-time {
    @include wh(100%, 36px);
    position: relative;
    // /deep/.el-input__inner {
    //   width: 100%;
    //   padding: 0 0 0 17px;
    //   border-radius: 7px;
    //   border: 1px solid #f4f0f3;
    // }
    // /deep/.el-date-editor {
    //   width: 100%;
    // }
    // > i {
    //   position: absolute;
    //   right: 4px;
    //   top: 50%;
    //   transform: translateY(-50%);
    //   color: #979698;
    // }
  }
  .tag-icon {
    width: 28px;
    display: flex;
    justify-content: center;
    align-items: center;
    > i {
      @include wh(7px, 2px);
      background-color: #979698;
    }
  }
  /deep/.el-dialog {
    border-radius: 7px;
  }
</style>
